package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Pref;
import cn.com.dashihui.web.service.CategoryService;
import cn.com.dashihui.web.service.GoodsBaseService;
import cn.com.dashihui.web.service.PrefService;

public class PrefController extends BaseController {

	private PrefService service = new PrefService();
	private CategoryService categoryService = new CategoryService();
	private GoodsBaseService goodsBaseService = Duang.duang(GoodsBaseService.class);

	/**
	 * 优惠券信息
	 */
	public void offer() {
		render("index.jsp");
	}

	/**
	 * 优惠券的分页数据
	 */
	public void page() {

		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		String id = getParaToInt("id") + "";
		int type = getParaToInt("type");
		renderResult(0, service.findByPage(pageNum, pageSize, id, type));
	}

	/**
	 * 优惠券添加页面
	 */
	public void toAdd() {

		render("add.jsp");
	}

	/**
	 * 添加优惠券
	 */
	public void doAdd() {
		// 优惠券类型
		int type = getParaToInt("typeselectpoint");
		// 优惠券标题
		String title = getPara("title");
		// 适用平台
		String usescope = getPara("scopeselectpoint");
		// 总发行量
		String count = getPara("count");
		// 发行时间
		String starttime = getPara("starttime");
		String endtime = getPara("endtime");

		// 有效期
		int validtime0 = getParaToInt("validtime0");
		// 有效时间
		String validstarttime = getPara("validstarttime");
		String validendtime = getPara("validendtime");
		// 固定天数
		String validday = getPara("validday");
		if (validtime0 == 1 && (StrKit.isBlank(validstarttime) || StrKit.isBlank(validendtime))) {
			renderResult(2);
			return;
		}
		if (validtime0 == 2 && StrKit.isBlank(validday)) {
			renderResult(3);
			return;
		}
		// 获取方式
		String method = getPara("method");
		// 面值
		String values = getPara("values");
		// 每人限领
		String limitcount = getPara("limitcount");
		// 适用门槛
		String uselimitmoney = getPara("uselimitmoney");
		// 适用范围
		int uselimitscope = getParaToInt("scope2");
		// 适用指定分类
		String checksfl = getPara("checksfl");
		// 适用指定商品
		String checkssp = getPara("checkssp");
		// 备注信息
		String comment = getPara("comment");

		// 获取门槛
		String getlimitmoney = getPara("getlimitmoney");
		// 可获得优惠券范围
		String getlimitscope = getPara("scope1");
		// 可获得指定分类
		String checksfl1 = getPara("checksfl1");
		// 可获得指定商品
		String checkssp1 = getPara("checkssp1");
		// 会员信息
		String huiyuan = getPara("huiyuan");
		// 折扣
		String discount = getPara("discount");

		// 保存
		Pref pref = new Pref().set("type", type).set("title", title).set("usescope", usescope)
				.set("createtime", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss")).set("method", method)
				.set("uselimitmoney", uselimitmoney).set("uselimitscope", uselimitscope).set("validtime0", validtime0);
		if (!StrKit.isBlank(count)) {

			pref.set("count", count);
		}
		if (!StrKit.isBlank(starttime) && !StrKit.isBlank(endtime)) {
			pref.set("starttime", starttime).set("endtime", endtime);
		}
		if (!StrKit.isBlank(limitcount)) {
			pref.set("limitcount", limitcount);
		}
		if (!StrKit.isBlank(comment)) {
			pref.set("comment", comment);
		}
		if (validtime0 == 1) {
			pref.set("validstarttime", validstarttime).set("validendtime", validendtime);
		} else {
			pref.set("validday", validday);
		}
		if (type == 5) {
			pref.set("discount", discount);
		} else {
			pref.set("values", values);
		}
		if (type == 4 && !StrKit.isBlank(huiyuan)) {
			pref.set("uselimitusersscope", huiyuan);
		}
		if (type == 1) {

			pref.set("getlimitmoney", getlimitmoney);
			pref.set("getlimitscope", getlimitscope);
		}
		if (uselimitscope == 2 && !StrKit.isBlank(checksfl)) {

			String[] checksflarray = getParaValues("checksfl");
			StringBuffer strfl = new StringBuffer();
			for (String s : checksflarray) {
				strfl.append(s + ",");
			}
			String strfla = strfl.toString();
			String uselimitcategories = strfla.substring(0, strfla.length() - 1);
			pref.set("uselimitcategories", uselimitcategories);
			int chooseNumfl = checksflarray.length;
			pref.set("uselimitcategoriessum", chooseNumfl);
		}
		if (uselimitscope == 3 && !StrKit.isBlank(checkssp)) {

			String[] checkssparray = getParaValues("checkssp");
			StringBuffer strsp = new StringBuffer();
			for (String s : checkssparray) {
				strsp.append(s + ",");
			}
			String strspa = strsp.toString();
			String uselimitgoods = strspa.substring(0, strspa.length() - 1);
			pref.set("uselimitgoods", uselimitgoods);
			int chooseNumsp = checkssparray.length;
			pref.set("uselimitgoodssum", chooseNumsp);
		}
		if ("2".equals(getlimitscope) && !StrKit.isBlank(checksfl1)) {

			String[] checksfl1array = getParaValues("checksfl1");
			StringBuffer strfl1 = new StringBuffer();
			for (String s : checksfl1array) {
				strfl1.append(s + ",");
			}
			String strfl1a = strfl1.toString();
			String getlimitcategories = strfl1a.substring(0, strfl1a.length() - 1);
			pref.set("getlimitcategories", getlimitcategories);
			int chooseNumfl1 = checksfl1array.length;
			pref.set("getlimitcategoriessum", chooseNumfl1);
		}
		if ("3".equals(getlimitscope) && !StrKit.isBlank(checkssp1)) {

			String[] checkssp1array = getParaValues("checkssp1");
			StringBuffer strsp1 = new StringBuffer();
			for (String s : checkssp1array) {
				strsp1.append(s + ",");
			}
			String strsp1a = strsp1.toString();
			String getlimitgoods = strsp1a.substring(0, strsp1a.length() - 1);
			pref.set("getlimitgoods", getlimitgoods);
			int chooseNumsp1 = checkssp1array.length;
			pref.set("getlimitgoodssum", chooseNumsp1);
		}

		if (service.addPref(pref)) {
			// renderSuccess(storeService.findById(store.getInt("id")));
			renderSuccess();
			return;
		}

		renderFailed();
	}

	/**
	 * （适用指定分类）查询分类列表
	 */
	public void chooseCategory() {
		setAttr("categoryList", categoryService.findAllCategory());

		render("categoryList.jsp");
	}

	/**
	 * （适用指定商品）查询商品列表
	 */
	public void chooseGood() {
		setAttr("goodList", goodsBaseService.findAllGoodsStore());
		render("goodList.jsp");
	}

	/**
	 * （可获得优惠券的指定分类）查询分类列表
	 */
	public void chooseCategorya() {
		setAttr("categoryLista", categoryService.findAllCategory());

		render("categoryLista.jsp");
	}

	/**
	 * （可获得优惠券的指定商品）查询商品列表
	 */
	public void chooseGooda() {
		setAttr("goodLista", goodsBaseService.findAllGoodsStore());
		render("goodLista.jsp");
	}

	public void toEdit() {

		int id = getParaToInt("id");
		System.err.println(id);
		if (id != 0) {
			Record object = service.detail(id);
			String getlimitcategories = object.getStr("getlimitcategories");
			String getlimitgoods = object.getStr("getlimitgoods");
			String uselimitcategories = object.getStr("uselimitcategories");
			String uselimitgoods = object.getStr("uselimitgoods");
			if (!StrKit.isBlank(getlimitcategories)) {
				List<Record> getLimitcategories = new ArrayList<>();
				String[] getLimitCategories = getlimitcategories.split(",");
				for (int i = 0; i < getLimitCategories.length; i++) {
					int categoryid = Integer.parseInt(getLimitCategories[i]);
					getLimitcategories.add(service.getCategoryById(categoryid).get(0));
				}
				if (getLimitcategories.size() > 0) {
					setAttr("getlimitcategories", getLimitcategories);
				}
			}
			if (!StrKit.isBlank(getlimitgoods)) {
				List<Record> getLimitgoods = new ArrayList<>();
				String[] getLimitGoods = getlimitgoods.split(",");
				for (int i = 0; i < getLimitGoods.length; i++) {
					int goodid = Integer.parseInt(getLimitGoods[i]);
					getLimitgoods.add(service.getGoodById(goodid));
				}
				if (getLimitgoods.size() > 0) {
					setAttr("getlimitgoods", getLimitgoods);
				}
			}
			if (!StrKit.isBlank(uselimitcategories)) {
				List<Record> useLimitcategories = new ArrayList<>();
				String[] useLimitCategories = uselimitcategories.split(",");
				for (int i = 0; i < useLimitCategories.length; i++) {
					int categoryid = Integer.parseInt(useLimitCategories[i]);
					useLimitcategories.add(service.getCategoryById(categoryid).get(0));
				}
				if (useLimitcategories.size() > 0) {
					setAttr("uselimitcategories", useLimitcategories);
				}
			}
			if (!StrKit.isBlank(uselimitgoods)) {
				List<Record> useLimitgoods = new ArrayList<>();
				String[] useLimitGoods = uselimitgoods.split(",");
				for (int i = 0; i < useLimitGoods.length; i++) {
					int goodid = Integer.parseInt(useLimitGoods[i]);
					useLimitgoods.add(service.getGoodById(goodid));
				}
				if (useLimitgoods.size() > 0) {
					setAttr("uselimitgoods", useLimitgoods);
				}
			}
			setAttr("object", object);
		}

		render("edit.jsp");
	}

	public void doEdit() {
		// 获取优惠券ID
		String id = getPara("id");
		// 优惠券类型
		int type = getParaToInt("typeselectpoint");
		// 优惠券标题
		String title = getPara("title");
		// 适用平台
		String usescope = getPara("scopeselectpoint");
		// 总发行量
		String count = getPara("count");
		// 发行时间
		String starttime = getPara("starttime");
		String endtime = getPara("endtime");
		// 有效期
		int validtime0 = getParaToInt("validtime0");
		// 有效时间
		String validstarttime = getPara("validstarttime");
		String validendtime = getPara("validendtime");
		// 固定天数
		String validday = getPara("validday");
		if (validtime0 == 1 && (StrKit.isBlank(validstarttime) || StrKit.isBlank(validendtime))) {
			renderResult(2);
			return;
		}
		if (validtime0 == 2 && StrKit.isBlank(validday)) {
			renderResult(3);
			return;
		}
		// 获取方式
		String method = getPara("method");
		// 面值
		String values = getPara("values");
		// 每人限领
		String limitcount = getPara("limitcount");
		// 适用门槛
		String uselimitmoney = getPara("uselimitmoney");
		// 适用范围
		int uselimitscope = getParaToInt("scope2");
		// 适用指定分类
		String checksfl = getPara("checksfl");
		// 适用指定商品
		String checkssp = getPara("checkssp");
		// 备注信息
		String comment = getPara("comment");

		// 获取门槛
		String getlimitmoney = getPara("getlimitmoney");
		// 可获得优惠券范围
		String getlimitscope = getPara("scope1");
		// 可获得指定分类
		String checksfl1 = getPara("checksfl1");
		// 可获得指定商品
		String checkssp1 = getPara("checkssp1");
		// 会员信息
		String huiyuan = getPara("huiyuan");
		// 折扣
		String discount = getPara("discount");

		// 更新
		Pref pref = service.findPref(Integer.valueOf(id)).set("type", type).set("title", title)
				.set("usescope", usescope).set("createtime", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"))
				.set("method", method).set("uselimitmoney", uselimitmoney).set("uselimitscope", uselimitscope)
				.set("validtime0", validtime0);
		if (!StrKit.isBlank(count)) {

			pref.set("count", count);
		}
		if (!StrKit.isBlank(starttime) && !StrKit.isBlank(endtime)) {
			pref.set("starttime", starttime).set("endtime", endtime);
		}
		if (!StrKit.isBlank(limitcount)) {
			pref.set("limitcount", limitcount);
		}
		if (!StrKit.isBlank(comment)) {
			pref.set("comment", comment);
		}
		if (validtime0 == 1) {
			pref.set("validstarttime", validstarttime).set("validendtime", validendtime);
			pref.set("validday", null);
		} else {
			pref.set("validday", validday);
			pref.set("validstarttime", null).set("validendtime", null);
		}
		if (type == 5) {
			pref.set("discount", discount);
		} else {
			pref.set("values", values);
		}
		if (type == 4 && !StrKit.isBlank(huiyuan)) {
			pref.set("uselimitusersscope", huiyuan);
		}
		if (type == 1) {

			pref.set("getlimitmoney", getlimitmoney);
			pref.set("getlimitscope", getlimitscope);
		}
		if (uselimitscope == 2 && !StrKit.isBlank(checksfl)) {

			String[] checksflarray = getParaValues("checksfl");
			StringBuffer strfl = new StringBuffer();
			for (String s : checksflarray) {
				strfl.append(s + ",");
			}
			String strfla = strfl.toString();
			String uselimitcategories = strfla.substring(0, strfla.length() - 1);
			pref.set("uselimitcategories", uselimitcategories);
			int chooseNumfl = checksflarray.length;
			pref.set("uselimitcategoriessum", chooseNumfl);
		}
		if (uselimitscope == 3 && !StrKit.isBlank(checkssp)) {

			String[] checkssparray = getParaValues("checkssp");
			StringBuffer strsp = new StringBuffer();
			for (String s : checkssparray) {
				strsp.append(s + ",");
			}
			String strspa = strsp.toString();
			String uselimitgoods = strspa.substring(0, strspa.length() - 1);
			pref.set("uselimitgoods", uselimitgoods);
			int chooseNumsp = checkssparray.length;
			pref.set("uselimitgoodssum", chooseNumsp);
		}
		if ("2".equals(getlimitscope) && !StrKit.isBlank(checksfl1)) {

			String[] checksfl1array = getParaValues("checksfl1");
			StringBuffer strfl1 = new StringBuffer();
			for (String s : checksfl1array) {
				strfl1.append(s + ",");
			}
			String strfl1a = strfl1.toString();
			String getlimitcategories = strfl1a.substring(0, strfl1a.length() - 1);
			pref.set("getlimitcategories", getlimitcategories);
			int chooseNumfl1 = checksfl1array.length;
			pref.set("getlimitcategoriessum", chooseNumfl1);
		}
		if ("3".equals(getlimitscope) && !StrKit.isBlank(checkssp1)) {

			String[] checkssp1array = getParaValues("checkssp1");
			StringBuffer strsp1 = new StringBuffer();
			for (String s : checkssp1array) {
				strsp1.append(s + ",");
			}
			String strsp1a = strsp1.toString();
			String getlimitgoods = strsp1a.substring(0, strsp1a.length() - 1);
			pref.set("getlimitgoods", getlimitgoods);
			int chooseNumsp1 = checkssp1array.length;
			pref.set("getlimitgoodssum", chooseNumsp1);
		}

		if (service.editPref(pref)) {
			// renderSuccess(storeService.findById(store.getInt("id")));
			renderSuccess(service.findPref(Integer.valueOf(id)));
			return;
		}

		renderFailed();

	}

	public void dele() {
		// 获取优惠券ID
		String id = getPara("id");
		System.out.println(id);
		// 更新
		Pref pref = service.findPref(Integer.valueOf(id)).set("status", 2);
		if (service.editPref(pref)) {
			// renderSuccess(storeService.findById(store.getInt("id")));
			renderSuccess(service.findPref(Integer.valueOf(id)));
			return;
		}

		renderFailed();

	}

	/**
	 * excel导出
	 */
	public void doExcel() {
		String operationMsg = "excel导出";
		logger.info(operationMsg);
		List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("id", "优惠券ID"));
		titles.add(new Pair("title", "优惠券标题"));
		titles.add(new Pair("type", "优惠券类型"));
		titles.add(new Pair("count", "发行总量"));
		titles.add(new Pair("limitcount", "每人限领数量"));
		titles.add(new Pair("getlimitmoney", "消费门槛"));
		titles.add(new Pair("values", "面值"));
		titles.add(new Pair("discount", "折扣"));
		titles.add(new Pair("createtime", "创建时间"));
		titles.add(new Pair("validstarttime", "生效时间"));
		titles.add(new Pair("validendtime", "失效时间"));
		titles.add(new Pair("validday", "有效天数"));

		int type = getParaToInt("type");
		String id = null;

		List<Record> list = service.findExcelByPage(type, id);
		List<Record> listz = new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record = new Record();
			record.set("id", list.get(i).get("id"));
			record.set("title", list.get(i).get("title"));
			if (list.get(i).get("type").equals(1)) {
				record.set("type", "购物赠券");
			} else if (list.get(i).get("type").equals(2)) {
				record.set("type", "注册赠券");
			} else if (list.get(i).get("type").equals(3)) {
				record.set("type", "全场赠券");
			} else if (list.get(i).get("type").equals(4)) {
				record.set("type", "会员赠券");
			} else {
				record.set("type", "打折赠券");
			}
			record.set("count", list.get(i).get("count"));
			record.set("limitcount", list.get(i).get("limitcount"));
			record.set("getlimitmoney", list.get(i).get("getlimitmoney"));
			record.set("values", list.get(i).get("values"));
			record.set("discount", list.get(i).get("discount"));
			record.set("createtime", list.get(i).get("createtime"));
			record.set("validstarttime", list.get(i).get("validstarttime"));
			record.set("validendtime", list.get(i).get("validendtime"));
			record.set("validday", list.get(i).get("validday"));
			listz.add(record);
		}
		// 导出
		service.export(getResponse(), getRequest(), titles, listz);
		logger.info("成功");
		renderNull();
	}

}
